package com.metagurukul.metagranth.actionservlets;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.metagurukul.metagranth.service.QuestionService;
import com.metagurukul.metagranth.vo.MetaData;

/**
 * Servlet implementation class SearchServlet This Servlet performs the
 * functionality of Searching a question operation.
 * 
 * @author team MetaGranth
 * 
 */
public class SearchQuestionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchQuestionServlet() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doPerform(request, response);
	}

	/**
	 * this function will be called whenever a question is to be searched.
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	private void doPerform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		if(session.getAttribute("userId")!=null){
		try {
			String byQuestion = null;
			byQuestion = request.getParameter("searchByQuestion");
			String byTag = null;
			byTag = request.getParameter("searchByTag");
			
			String errorMessage = null;
			RequestDispatcher rd = null;
			boolean flag1 = false;
			boolean flag2 = false;
			ArrayList<MetaData> questionList = new ArrayList<MetaData>();

			if (byQuestion == null) {
				flag1 = true;
			}
			if (byTag == null) {
				flag2 = true;
			}
			if (!flag1 && !flag2) {
				if (byQuestion.trim().equals("")) {
					if (byTag.trim().equals("")) {
						errorMessage = "Both the fields cannot be left blank";
						request.setAttribute("errmsg", errorMessage);
						rd = request.getRequestDispatcher("home.jsp");
					} else {

						questionList = QuestionService.searchByTag(byTag);
						request.setAttribute("metaDataList", questionList);
						rd = request.getRequestDispatcher("search-result.jsp");
					}

				} else {
					if (byTag.trim().equals("")) {
						questionList = QuestionService.searchByQuestion(byQuestion);
						request.setAttribute("metaDataList", questionList);
						rd = request.getRequestDispatcher("search-result.jsp");
					} else {
						questionList = QuestionService.searchByQuestionAndTag(
								byQuestion, byTag);
						request.setAttribute("metaDataList", questionList);
						rd = request.getRequestDispatcher("search-result.jsp");
					}
				}
				request.setAttribute("questionValue", byQuestion);
				request.setAttribute("tagValue", byTag);

			} else {
				errorMessage = "Both the fields cannot be left blank";
				request.setAttribute("errmsg", errorMessage);
				rd = request.getRequestDispatcher("home.jsp");
			}

			rd.forward(request, response);
		} catch (Exception exception) {
			request.setAttribute("errormsg", exception.getMessage());
			request.getRequestDispatcher("errorPage.jsp").forward(request,
					response);
		}
		}else{
			response.sendRedirect("index.jsp");
		}
	}
}
